package com.moment.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.moment.model.UserReg;
import com.moment.model.WalletData;
import com.moment.util.Converter;
import com.moment.webservice.Services;
import com.moment.webservice.ServicesProxy;

/**
 * Servlet implementation class TransactionController
 */
@WebServlet("/TransactionController")
public class TransactionController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TransactionController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		dotransaction(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		dotransaction(request, response);
	}

	protected void dotransaction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String password = request.getParameter("password").trim();
		String custMobile = request.getParameter("custMobile").trim();
		String merMobile = request.getParameter("merMobile").trim();
		String nameInstru = request.getParameter("nameInstru").trim();
		String amt = request.getParameter("amt").trim();
		String ref = request.getParameter("ref").trim();
		String nid = request.getParameter("nid").trim();
		String lat = request.getParameter("lat").trim();
		String longi = request.getParameter("longi").trim();
		JSONArray jArray;
		JSONObject jobj;
		String msg = null, transId = null;

		System.out
				.println("custMobile=" + custMobile + " password" + password
						+ "merMobile=" + merMobile + " nameInstru" + nameInstru
						+ "amt=" + amt + " lat" + lat + "longi=" + longi
						+ " nid" + nid);

		/*
		 * if (lat == null || longi == null || lat.equals("") ||
		 * longi.equals("")) {
		 * response.sendRedirect("/MMP/jsp/index.jsp?error=null"); } else {
		 */
		HttpSession session=request.getSession();
		//--New CODE-------------
		Map<String,String> map=new HashMap<String,String>();
		map.put("method", "7");
	map.put("mnum1",custMobile);
	map.put("mnum2",merMobile);
	map.put("nickname",nameInstru);
	map.put("password",password);
				map.put("amount",amt);
				map.put("refnumber",ref);
				map.put("nid",nid);
				map.put("lat",lat);
				map.put("longi",longi);
		String json=new Converter().mapToJson(map);
		System.out.println("json String :"+json);
		Services serviceproxy=new ServicesProxy();
		
		String ss=serviceproxy.service(json);
//---------------------------
//		WebsProxy proxy = new WebsProxy();
//
//		String ss = proxy.transaction1(custMobile, merMobile, nameInstru,
//				password, amt, ref, nid, lat, longi);
		System.out.println("Data From webservices" + ss);
		if (ss.equals("error")) {
			System.out.println("Fail--0");
			
			//request.getRequestDispatcher("jsp/viewprofile.jsp").forward(request, response);
			response.sendRedirect("/MMP/jsp/viewprofile.jsp");
			
		}
		if (ss.equals("Fail")) {
			System.out.println("Fail--");
			
			//request.getRequestDispatcher("jsp/viewprofile.jsp").forward(request, response);
			response.sendRedirect("/MMP/jsp/viewprofile.jsp");
			
		} else {
			if (!("[]").equalsIgnoreCase(ss)) {
				System.out.println("Fail--2");
				try {
					jArray = new JSONArray(ss);
					jobj = jArray.getJSONObject(0);
					msg = jobj.getString("msg");
					if (msg.equalsIgnoreCase("Connectiondone")) {
						System.out.println("Fail--3");
						transId = jobj.getString("tid");
						//--New CODE-------------
						Map<String,String> map1=new HashMap<String,String>();
						map1.put("method", "8");
					map1.put("tid",transId);
					map1.put("sign",null);
						String json1=new Converter().mapToJson(map1);
						System.out.println("json String :"+json1);
						Services serviceproxy1=new ServicesProxy();
						
						String output=serviceproxy1.service(json1);
			//---------------------------
						
						
						
						//String output = proxy.sendSignature(transId, null);
						if (output.equals("Success")) {
							System.out.println("Fail--4");
							session.setAttribute("tid",transId);
							//request.getRequestDispatcher("jsp/receipt.jsp").forward(request, response);
							response.sendRedirect("/MMP/jsp/receipt.jsp");
							
						} else {
							System.out.println("Fail--5");
							session.setAttribute("tid",transId);
						//	request.getRequestDispatcher("jsp/receipt.jsp").forward(request, response);
							response.sendRedirect("/MMP/jsp/receipt.jsp");
						}
					} else {
						System.out.println("Fail--6");
						System.out.println("msg="+msg);
					
						session.setAttribute("error",msg);
						//request.getRequestDispatcher("jsp/viewprofile.jsp").forward(request, response);
						response.sendRedirect("/MMP/jsp/viewprofile.jsp");
					}
				} catch (JSONException e) {
					
					e.printStackTrace();
				}catch(java.net.NoRouteToHostException e){
					session.setAttribute("error", "NO CONNECTION TO SERVER");
					//request.getRequestDispatcher("jsp/viewprofile.jsp").forward(request, response);
					
					response.sendRedirect("/MMP/jsp/viewprofile.jsp");
				}catch(Exception e ){
					session.setAttribute("error", "CONNECTION LOST");
					//request.getRequestDispatcher("jsp/viewprofile.jsp").forward(request, response);
					
				response.sendRedirect("/MMP/jsp/viewprofile.jsp");
								
				}
			} else {
				// ///////////////////
			}
		}
		// }
	}
}
